package com.drugStorage.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.drugStorage.domain.DetailDrugStorageList;
import com.drugStorage.hibernate.Utils.HibernateUtils;
import com.drugStorage.hibernate.dao.DetailDrugStorageListDao;

public class DetailStorageListDaoImpl implements DetailDrugStorageListDao {

	private static final String LOAD_ALL = "from DetailDrugStorageList";
	public void SaveDetailDrugStorageList(
			DetailDrugStorageList detailDrugStorageList) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.persist(detailDrugStorageList);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}

	public void UpdateDetailDrugStorageList(
			DetailDrugStorageList detailDrugStorageList) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.saveOrUpdate(detailDrugStorageList);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}

	public DetailDrugStorageList getDetailDrugStorageListById(int id) {
		Session s = null;

		try {
			s = HibernateUtils.getSession();

			Criteria c = s.createCriteria(DetailDrugStorageList.class);
			c.add(Restrictions.eq("id", id));
			DetailDrugStorageList detailDrugStorageList = (DetailDrugStorageList) c.uniqueResult();
			return detailDrugStorageList;

		} finally {
			if (s != null)
				s.close();
		}
	}

	public DetailDrugStorageList getDetailDrugStorageListByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<DetailDrugStorageList> getDetailDrugStorageLists() {
		Session s = null;
		try{
			s = HibernateUtils.getSession();
			Query q = s.createQuery(LOAD_ALL);
			return q.list();
		}finally{
			if(s!=null)
				s.close();
		}
	}

}
